package com.sixdoors.web.controller.system;

import com.sixdoors.common.core.controller.BaseController;
import com.sixdoors.common.core.domain.AjaxResult;
import com.sixdoors.common.core.domain.model.RegisterBody;
import com.sixdoors.common.utils.StringUtils;
import com.sixdoors.framework.web.service.SysRegisterService;
import com.sixdoors.system.service.ISysConfigService;
import com.sixdoors.web.controller.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * 注册验证
 * 
 * @author sixdoors
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;



    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     *  获取邮箱验证码
     *
     */
    @GetMapping("/getEmailVerificationCode")
    public AjaxResult getEmailVerificationCode(@RequestParam String email)
    {
        if (!email.contains("@qq.com")) return AjaxResult.error("请输入qq邮箱地址！");

        Set<String> set = new HashSet<>();
        set.add(email);
        MailUtils.sendEmail(set, "测试发送邮件的接口！",
                "您好！这是我发送的一封测试发送接口的邮件，看完请删除记录。");
        return success("发送成功！");
    }
}
